package com.kafeblog.roo.portletmvc;

import java.util.logging.Logger;

import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.Service;
import org.osgi.service.component.ComponentContext;
import org.springframework.roo.model.JavaType;
import org.springframework.roo.shell.CliAvailabilityIndicator;
import org.springframework.roo.shell.CliCommand;
import org.springframework.roo.shell.CliOption;
import org.springframework.roo.shell.CommandMarker;
import org.springframework.roo.shell.converters.StaticFieldConverter;
import org.springframework.roo.support.logging.HandlerUtils;

import com.kafeblog.roo.portletmvc.liferay.LiferayOperations;

/**
 * This addons provide following commands:
 * + portlet setup			Setup PortletMVC, included: Maven, portlet.xml and other required
 * + portlet add		Create portlet scaffold, included Controller, Views
 *   
 * @since 1.1.1
 */
@Component
@Service
public class PortletmvcCommands implements CommandMarker {
	@Reference private PortletmvcOperations operations;
	@Reference private LiferayOperations liferayOperations; 
    
    @Reference private StaticFieldConverter staticFieldConverter;

    private Logger log = HandlerUtils.getLogger(getClass());
    protected void activate(ComponentContext context) {
        staticFieldConverter.add(PortletmvcPropertyName.class);
    }
    protected void deactivate(ComponentContext context) {
        staticFieldConverter.remove(PortletmvcPropertyName.class);
    }
    
    /*********** Command: portlet setup ***********/
    @CliAvailabilityIndicator("portlet setup")
    public boolean isSetupCommandAvailable() {
    	return operations.isSetupCommandAvailable();
    }
    
    @CliCommand(value = "portlet setup", help = "Setup Spring PortletMVC to web component")
    public void performSetupCommand() {
    	operations.performSetupCommand();	
    }
   
    /*********** Command: portlet liferay ***********/
    @CliAvailabilityIndicator("portlet liferay")
    public boolean isLiferayCommandAvailable() {
    	return liferayOperations.isLiferayCommandAvailable();
    }
    
    @CliCommand(value = "portlet liferay", help = "Configure this portlet to support Liferay portal")
    public void performLiferayCommand(
    		@CliOption(key = "version", mandatory = false, help = "Liferay portal version", specifiedDefaultValue="6.1.1", unspecifiedDefaultValue="6.1.1") final String liferayVersion
    		) {
    	liferayOperations.configureLiferayPortal(liferayVersion);	
    }
    
    /*********** Command: portlet add ***********/
    @CliAvailabilityIndicator("portlet add")
    public boolean isAddCommandAvailable() {
        return operations.isAddCommandAvailable();
    }
    
    @CliCommand(value = "portlet add", help="Generate portlet")
    public void performAddCommand(
    		@CliOption(key = "name", mandatory = true, help = "Name of the portlet") final JavaType className,
    		@CliOption(key = "displayName", mandatory = false, help = "The portlet display name, use name if not specified") final String displayName,
    		@CliOption(key = "category", mandatory = false, help = "Category of the portlet, 'Undefined' will be use if empty") final String category,
    		@CliOption(key = "supportModes", mandatory = false, help = "Portlet mode which this portlet supports. Default: 'view,edit,help'") final String supportModes
    		) {
    	log.info("performAddCommand");
        operations.performAddCommand(className, supportModes, displayName, category);
		liferayOperations.updateLiferayDisplayXml(className, category);
		liferayOperations.updateLiferayPortletXml(className, displayName, supportModes);

    }
}